import pickle
from typing import List, Union, Optional

from fastapi import Query
from pydantic import BaseModel, validator

from app.schemas.common import DateTimeModelMixin


class SetImages(BaseModel):
    images: Union[List[str], str, None]

    @validator('images', pre=True)
    def split_str(cls, v):
        """如果传参是字符串，根据逗号切割成list"""
        print(" ".join('%s' % id for id in v))
        if isinstance(v, list):
            return " ".join('%s' % id for id in v)
        return list(v)


class Config:
    orm_mode = True


class UserId(BaseModel):
    id: str
    openid: str


class UserInfo(DateTimeModelMixin, SetImages, UserId):
    sex: Union[int, None]
    province: Union[int, None]
    country: Union[int, None]
    name: Union[str, None]
    age: Union[int, None]
    city: Union[int, None]
    avatar: Union[str, None]
    education: Union[str, None]
    height: Union[int, None]
    income: Union[int, None]
    major: Union[str, None]
    is_house: Union[int, None] = Query(ge=0, le=1, default=0)
    strengths: Union[str, None]
    blood_type: Union[str, None]
    is_marry: Union[int, None] = Query(ge=0, le=1, default=0)
    other: Union[str, None]
    province: Union[int, None]
    city: Union[int, None]
    country: Union[int, None]
    avatar: Union[str, None]
    latitude: Union[float, None]
    longitude: Union[float, None]

    class Config:
        orm_mode = True
        arbitrary_types_allowed = True


class Item(UserInfo):
    is_friends: bool
    distance_km: float


class UserNearbyList(BaseModel):
    list: Union[List[Item], None]
    total: int = 0

    class Config:
        orm_mode = True


class UserHistoryLocationSM(DateTimeModelMixin):
    latitude: float
    longitude: float

    class Config:
        orm_mode = True
